import type { Result } from "#/api";
import apiClient from "../../apiClient";
import type { AutoCodeRule, AutoCodePart } from "@/pages/tool/autoRule/entity";

export interface PageListReq {
  //规则编码
  ruleCode?: string;
  //规则名称
  ruleName?: string;
  //当前页码
  page: number;
  //每页数量
  pageSize: number;
}

export interface PageListRes {
  //总记录数
  total: number;
  //数据列表
  records: AutoCodeRule[];
}

const pageList = (data: PageListReq): Promise<PageListRes> =>
  apiClient.post({
    url: "/tool/rule/pageList",
    data
});

const selectList = (): Promise<AutoCodeRule[]> =>
  apiClient.get({
    url: "/tool/rule/selectList"
});

const getInfo = (id: React.Key): Promise<AutoCodeRule> =>
  apiClient.get({
    url: `/tool/rule/${id}`
});

const add = (data: AutoCodeRule): Promise<AutoCodeRule> =>
  apiClient.post({
    url: "/tool/rule",
    data
});

const update = (data: AutoCodeRule): Promise<AutoCodeRule> =>
  apiClient.put({
    url: "/tool/rule",
    data
});

const remove = (ids: React.Key[]): Promise<void> =>
  apiClient.delete({
    url: "/tool/rule",
    params: {
    ids: ids.join(",")
  }
});

//获取规则组成部分
const getRulePart = (ruleId: React.Key): Promise<AutoCodePart[]> =>
  apiClient.get({
    url: `/tool/part/list/${ruleId}`
  })

const savePart = (partList: AutoCodePart[]): Promise<Result> => 
  apiClient.post({
    url: "/tool/part",
    data: partList
  })  

export const autoCodeRuleService = {
  pageList,
  selectList,
  getInfo,
  add,
  update,
  remove,
  getRulePart,
  savePart
};

export default autoCodeRuleService;